package com.mzy.basics.network;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/5/19 20:27
 * @Version 1.0
 */
public class demo2 {
    /**
     * 获取操作系统上所有的网卡，不管是启用还是没启用（有真实物理地址的）
     * 按照网络接口划分，一张网卡可能有多个网络接口（不可用来筛选网络接口）
     * @param args
     * @throws SocketException
     */
    public static void main(String[] args) throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface ni = networkInterfaces.nextElement();
            if(ni.isUp())
                continue;

            byte[] hardwareAddress = ni.getHardwareAddress();
            if (hardwareAddress != null) {
                String[] hexadecimalFormat = new String[hardwareAddress.length];
                for (int i = 0; i < hardwareAddress.length; i++) {
                    hexadecimalFormat[i] = String.format("%02X", hardwareAddress[i]);
                }
                System.out.println(ni.getDisplayName() + " " + String.join("-", hexadecimalFormat));
            }
        }
    }
}
